#!/bin/bash
scriptWorkd=$(cd "$(dirname $0)";pwd)

### ========= 全局变量 ===========
    software=("build-essential" "unzip" "zip" "cmake" "xclip"
                    "manpages-zh" "tree" "xclip" "flatpak"
                    "tmux" "ssh" "git" "eject" "net-tools" "curl" "vim" "p7zip" "p7zip-full" "mlocate" "locate"
                    "gdb" "busybox" "python3-pip" "fim" "cron" 
                    "gcc-11" "g++-11" "exfat-fuse" "ntfs-3g" 
                    "python3.8" "exfat-utils" "python-is-python3"
                    "onedrive" "onedriver" "fcitx5-frontend-gtk2" 
                    "playonlinux" "python3-pyasyncore" "libgl1-mesa-dev" "mesa-utils" "samba" "winbind"
                    "winetricks" "python3-pynvim" "pipx"
                    "trash-cli"
                )
    systemType=$(awk -F= '/^ID=/{print $2}' /etc/os-release)
### ==============================

### ========= 前置文件 ===========
if [[ -f ${HOME}/bin/basic_func.sh ]];then  ### 存在才载入
    source ${HOME}/bin/basic_func.sh
elif [[ -f ./bin/basic_func.sh ]];then
    source ./bin/basic_func.sh
elif [[ -f ../bin/basic_func.sh ]];then
    source ../bin/basic_func.sh
fi
### ==============================


function main() {
    ## 开始信息
    echo -e "${blue4}\n$(basename $0)\n${normal}"

    ##
set +e

    sudo apt update
    echo -e "${hintStrProcessing}\n添加ppa源并更换为国内镜像\n${normal}"
    echo -e "${hintStrProcessing}\nadding ppa repo and change its sources to mirrors\n${normal}"
    sudo apt-get install --reinstall ca-certificates
    sudo update-ca-certificates
    sudo -E add-apt-repository ppa:ubuntu-toolchain-r/test -ys
    sudo -E add-apt-repository ppa:neovim-ppa/unstable -ys

    ## 修改PPA源镜像
    for item in $(find /etc/apt/sources.list.d -type f ! -name "*.bak");do
        echo $item
        
        cat $item | grep -qE 'ppa\.launchpad\.net|ppa\.launchpadcontent\.net'
        before=$?

        sudo sed -i "s/ppa.launchpad.net/launchpad.proxy.ustclug.org/g" $item
        sudo sed -i "s/ppa.launchpadcontent.net/launchpad.proxy.ustclug.org/g" $item

        cat $item | grep -qE 'ppa\.launchpad\.net|ppa\.launchpadcontent\.net'
    
        if [[ $? != $before ]]; then
            echo -e "${hintStrSucceed}镜像已修改$(basename $item)"
        fi
    done
        
    # if [[ -e /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-test-focal.list ]];then
    #     sudo sed -i "s/ppa.launchpad.net/launchpad.proxy.ustclug.org/g" /etc/apt/source.list.d/ubuntu-toolchain-r-ubuntu-test-focal.list
    #     echo -e "${hintStrSucceed}更换成功[toolchain]"
    # else
    #     echo -e "${hintStrWarning}无文件"
    # fi
    #
    # if [[ -e /etc/apt/sources.list.d/neovim-ppa-ubuntu-stable-focal.list ]];then
    #     sudo sed -i "s/ppa.launchpad.net/launchpad.proxy.ustclug.org/g" /etc/apt/source.list.d/neovim-ppa-ubuntu-stable-focal.list
    #     echo -e "${hintStrSucceed}更换成功[neovim]"
    # else
    #     echo -e "${hintStrWarning}无文件"
    # fi

    echo -e "${hintStrProcessing}\n更新apt中\n${normal}"
    echo -e "${hintStrProcessing}\nupdating apt\n${normal}"
    sudo apt dist-upgrade -y

    # ### 添加软件源
    # if [[ ${systemType} == "ubuntu" ]];then
    #     sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
    # fi

    set +e
    ### 一个一个安装
    for pkg in ${software[@]};do
        echo -e "${hintStrProcessing}Installing $pkg."
        if sudo apt-get install -y "$pkg"; then
            echo -e "${hintStrSucceed}$pkg installed successfully."
        else
            echo -e "${hintStrFailed}Failed to install $pkg."
        fi
    done

    # echo -e "${hintStrProcessing}\n安装nvim包管理器packer\n${normal}"
    # echo -e "${hintStrProcessing}\nInstalling packer\n${normal}"
    # if ! [[ -d ~/.local/share/nvim/site/pack/packer/start/packer.nvim ]]; then
	   #  lastDir=$(pwd)
	   #  mkdir -p ~/.local/share/nvim/site/pack/packer/start/
	   #  cd ~/.local/share/nvim/site/pack/packer/start/
	   #  gitClone wbthomason/packer.nvim
	   #  cd ${lastDir}
    #     # git clone --depth 1 ${proxy}/https://github.com/wbthomason/packer.nvim\
 	  #       # ~/.local/share/nvim/site/pack/packer/start/packer.nvim
    # fi
    echo -e "${hintStrProcessing}\n安装tmux包管理器tpm\n${normal}"
    echo -e "${hintStrProcessing}\nInstalling tpm\n${normal}"
    if ! [[ -d ~/.tmux/plugins/tpm ]]; then
	    lastDir=$(pwd)
	    mkdir -p ~/.tmux/plugins
	    cd ~/.tmux/plugins
	    gitClone tmux-plugins/tpm
	    cd ${lastDir}
            # git clone ${proxy}/https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
    fi


    # echo -e "${hintStrProcessing}\n设置flatpak中\n${normal}"
    # echo -e "${hintStrProcessing}\nSetting Flatpak.\n${normal}"
    # flatpak remote-add --if-not-exists --user flathub https://mirror.sjtu.edu.cn/flathub

    ## 结束信息
    echo -e "${green4}\n$(basename $0)\n${normal}"
}
main $@
